﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using SIPO.Library.DataAccess.Report;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Web.Support;
using Microsoft.Reporting.WebForms;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Drawing;

namespace SIPO.Web.lists.Report
{
    public partial class rptPenerimaanlist : BasePage
    {
        recSectorRepo recSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recBillingRepo recBillingDal = new recBillingRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        //recDayOffRepo recDayOffDal = new recDayOffRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);


        protected override void OnLoad(EventArgs e)
        {
           
            if (!IsPostBack)
            {

            //    List<recSector> sectors = recSectorDal.GetList();
            //    ddlSector.Items.Clear();
            //    sectors.ForEach(delegate(recSector sector)
            //    {
            //        ddlSector.Items.Add(new ListItem(sector.SectorCode + " - " + sector.SectorName, sector.SectorID.ToString()));
            //    });
            //    ddlSector.Items.Insert(0, new ListItem("-- semua sektor --", string.Empty));
            //    ddlSubSector.Items.Insert(0, new ListItem("-- semua sub sektor --", string.Empty));
                hfPeriode2.Value = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
                hfPeriode.Value = "1/1/" + DateTime.Now.Year;
                
            //}
            //if (ddlSector.SelectedValue != "")
            //{
            //    List<recSubSector> subSector = recSubSectorDal.GetList().FindAll(x => x.SectorID == int.Parse(ddlSector.SelectedValue));
            //    ddlSubSector.Items.Clear();
            //    subSector.ForEach(delegate(recSubSector subsector)
            //    {
            //        ddlSubSector.Items.Add(new ListItem(subsector.SubSectorCode + " - " + subsector.SubSectorName, subsector.SubSectorID.ToString()));
            //    });
            //    if (hfSub.Value != "")
            //    {
            //        ddlSubSector.SelectedValue = hfSub.Value;
            //    }
            //    ddlSubSector.Items.Insert(0, new ListItem("-- semua sub sektor --", string.Empty));
               
            }
           
        }
        

        protected void Button1_Click(object sender, EventArgs e)
        {

            string txtPeriode = hfPeriode.Value;
            string txtPeriode2 = hfPeriode2.Value;
           // string Sektor = ddlSector.SelectedValue;
            string SubSektor = hfSub.Value;
            string param = string.Empty;

           

            //param += " Where transactions.recBilling.BillingDate between CONVERT(DATETIME,'" + txtPeriode + "', 103) and CONVERT(DATETIME,'" + txtPeriode2 + "', 103)";
            //if (Sektor != "")
            //{
            //    param += " and master.recSector.Sectorid = " + Sektor;
            //}
            //if (SubSektor != "")
            //{
            //    param += " and master.recSubSector.SubSectorId=" + SubSektor;
            //}
            //string versi = hfVersi.Value;
            ////Ambil Termin Due Date

            //List<string> listTermin = recBillingDal.GetTerminDueDate();
            //foreach (string terminDate in listTermin)
            //{
            //    //DateTime dateCurrent = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
            //    DateTime dateCurrent = Convert.ToDateTime(recDayOffDal.GetCurrentDateFromRecParameter().ToString("dd/MM/yyyy"));
            //    DateTime dateTermin = Convert.ToDateTime(terminDate + "/" + dateCurrent.ToString("yyyy"));

            //    if (dateCurrent <= dateTermin)
            //    {
            //        parameterTerminDueDate = " AND recBilling.DueDate <= CONVERT(DATETIME, '" + dateTermin.ToString("dd/MM/yyyy") + "', 103)";
            //        break;
            //    }
            //}


            //string parameter = string.Empty;
            //string parameter2 = string.Empty;
            //parameter2 = " and recBilling.BillingDate between " + "CONVERT(DATETIME,'01/01/" + txtPeriode.Substring(6, 4) + "', 103) and CONVERT(DATETIME,'" + txtPeriode + "', 103)";
            //int paramdate3 = int.Parse(txtPeriode.Substring(6, 4)) - 1;
            //parameter = "= '" + txtPeriode.Substring(6, 4) + "' and transactions.recacceptancedetail.AcceptanceId = (select acceptanceid from transactions.recAcceptance where acceptanceperiod='" + txtPeriode.Substring(6, 4) + "' and versi ='"+ versi +"')";
            //string parameter3 = "= '" + paramdate3 + "' and transactions.recacceptancedetail.AcceptanceId = (select acceptanceid from transactions.recAcceptance where acceptanceperiod='" + txtPeriode.Substring(6, 4) + "' and versi ='" + versi + "')";
            //string parameter4 = " and recBilling.BillingDate between " + "CONVERT(DATETIME,'01/01/" + paramdate3 + "', 103) and CONVERT(DATETIME,'" + txtPeriode.Replace(txtPeriode.Substring(6, 4),paramdate3.ToString()) + "', 103)";
            
            ////string parameter2 = "between " + ddlPeriode0.SelectedValue + " and " + ddlPeriode1.SelectedValue;
            //    string period = "Tahun " + ddlPeriode0.SelectedValue + " s.d " + ddlPeriode1.SelectedValue;
            //    if (ddlPeriode0.SelectedValue == ddlPeriode1.SelectedValue)
            //    {
            //        period = "Tahun " + ddlPeriode0.SelectedValue;
            //    }

            //    if (ddlPeriode0.SelectedValue == ddlPeriode1.SelectedValue)
            //    {
            //        parameter = "= " + ddlPeriode0.SelectedValue +" and Versi=" + ddlVersi.SelectedValue;
            //    }

               
                try
                {
                    string hasil = recBillingDal.GenReport(txtPeriode, txtPeriode2);
                    DataTable dt = recBillingDal.GetListReportUmurPiutang(param);
                    rsViewer.Reset();
                    string path = "~/reportviewer/Transaction/rptUmurPiutang.rdlc";
                    rsViewer.LocalReport.ReportPath = Server.MapPath(path);
                    ReportDataSource rds = new ReportDataSource();
                    rds.Name = "DataSet1";
                    rds.Value = dt;
                   ReportParameter p1 = new ReportParameter("Periode", "Periode "+ txtPeriode +" s.d "+txtPeriode2);
                    ////ReportParameter p2 = new ReportParameter("PeriodeThn1", txtPeriode.Substring(6, 4));
                    ////ReportParameter p3 = new ReportParameter("PeriodeThn2", paramdate3.ToString());
                   this.rsViewer.LocalReport.SetParameters(new ReportParameter[] { p1 });

                    rsViewer.LocalReport.DataSources.Clear();
                    rsViewer.LocalReport.DataSources.Add(rds);
                    rsViewer.LocalReport.Refresh();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            



            
          
            
            
               
            

        }

        


    }
}